package com.example.exam.sysRole.controller;

import com.example.exam.authorization.service.AuthorizationService;
import com.example.exam.common.ResultBean;
import com.example.exam.common.ResultBeanCodeEnum;
import com.example.exam.db.PageInfoBean;
import com.example.exam.sysRole.model.SysRoleBean;
import com.example.exam.sysRole.service.SysRoleService;
import com.example.exam.user.service.UserRoleService;
import com.example.exam.utils.StringToList;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@RequestMapping("/sysRoleController")
public class SysRoleController {
    private static String base_path="/sysRole/";
    @Autowired
    SysRoleService srs;
    @Autowired
    UserRoleService userRoleService;
    @Autowired
    AuthorizationService authorizationService;
    @GetMapping("/toUpdateRole")
        /** 
            * @Description:  去修改角色页面
            * @Param: [srb, request] 
            * @return: java.lang.String 
            * @Author: xhw
            * @Date: 2021/7/26 
            */ 
    public String toUpdateRole(@ModelAttribute SysRoleBean srb, HttpServletRequest request){
        if(srb.getId()!=null){
            SysRoleBean srb2=srs.selectOneRole(srb.getId());
            request.setAttribute("srb",srb2);
        }
        return base_path+"updateRole";
    }
    @GetMapping("/toAddRole")
        /** 
            * @Description: 去添加角色页面
            * @Param: [] 
            * @return: java.lang.String 
            * @Author: xhw
            * @Date: 2021/7/26 
            */ 
    public String toAddRole(){
        return base_path+"addRole";
    }
    @GetMapping("/toSysRole")
        /** 
            * @Description: 去角色信息列表页面
            * @Param: [request] 
            * @return: java.lang.String 
            * @Author: xhw
            * @Date: 2021/7/26 
            */ 
    public String toSysRole(HttpServletRequest request){
        HttpSession session=request.getSession();
        Integer userId=(Integer) session.getAttribute("id");
        Integer id=(Integer)session.getAttribute("id");
        String post=authorizationService.findPost(id);
        Integer num=authorizationService.findRole(id);
        return base_path+"sysRole";
    }
    @PostMapping("/insertRole")
    @ResponseBody
        /** 
            * @Description:  添加角色信息
            * @Param: [srb, request] 
            * @return: com.example.exam.common.ResultBean 
            * @Author: xhw
            * @Date: 2021/7/26 
            */ 
    public ResultBean insertRole(SysRoleBean srb, HttpServletRequest request){
        ResultBean result = new ResultBean();
        HttpSession session=request.getSession();
        Integer userId=(Integer) session.getAttribute("id");
        srb.setCreateUser(userId);
        srb.setCreateTime(new Date());
        try {
            Integer i=srs.insertRole(srb);
            result.fillData(i);
            if(i == 1) {
                result.setCode(ResultBeanCodeEnum.OPERA_SUCCESS.getCode());
                result.setMsg(ResultBeanCodeEnum.OPERA_SUCCESS.getMsg());
            }else {
                result.setCode(ResultBeanCodeEnum.OPERA_FAIL.getCode());
                result.setMsg(ResultBeanCodeEnum.OPERA_FAIL.getMsg());
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(ResultBeanCodeEnum.OPERA_EXCEPTION.getCode());
            result.setMsg(ResultBeanCodeEnum.OPERA_EXCEPTION.getMsg());
        }
        return result;
    }
    @PostMapping("/deleteRole")
    @ResponseBody
        /** 
            * @Description:  删除角色信息
            * @Param: [srb] 
            * @return: com.example.exam.common.ResultBean 
            * @Author: xhw
            * @Date: 2021/7/26 
            */ 
    public ResultBean deleteRole(@ModelAttribute SysRoleBean srb){
        ResultBean result = new ResultBean();
        try {
            Integer i=srs.deleteRole(srb.getId());
            result.fillData(i);
            if(i == 1) {
                result.setCode(ResultBeanCodeEnum.OPERA_SUCCESS.getCode());
                result.setMsg(ResultBeanCodeEnum.OPERA_SUCCESS.getMsg());
            }else {
                result.setCode(ResultBeanCodeEnum.OPERA_FAIL.getCode());
                result.setMsg(ResultBeanCodeEnum.OPERA_FAIL.getMsg());
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(ResultBeanCodeEnum.OPERA_EXCEPTION.getCode());
            result.setMsg(ResultBeanCodeEnum.OPERA_EXCEPTION.getMsg());
        }
        return result;
    }
    @PostMapping("/selectRole")
    @ResponseBody
        /** 
            * @Description:  角色信息列表
            * @Param: [request, page, limit] 
            * @return: com.example.exam.common.ResultBean 
            * @Author: xhw
            * @Date: 2021/7/26 
            */ 
    public ResultBean selectRole(HttpServletRequest request,@RequestParam("page") Integer page,@RequestParam("limit") Integer limit){
        ResultBean resultBean = new ResultBean();
        String role_name = request.getParameter("name");
        HttpSession session=request.getSession();
        Integer id=(Integer) session.getAttribute("id");
        Integer roleId=authorizationService.findRole(id);
        PageInfoBean pageInfoBean = null;
        try {
            pageInfoBean = srs.getRoleList(page,limit,role_name,roleId);
            resultBean.fillData(pageInfoBean);
            resultBean.setCode(ResultBeanCodeEnum.OPERA_SUCCESS.getCode());
            resultBean.setMsg(ResultBeanCodeEnum.OPERA_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            resultBean.setCode(ResultBeanCodeEnum.OPERA_FAIL.getCode());
            resultBean.setMsg(ResultBeanCodeEnum.OPERA_FAIL.getMsg());
        }
        return resultBean;
    }
    @PostMapping("/updateRole")
    @ResponseBody
        /** 
            * @Description: 修改角色信息
            * @Param: [srb, request] 
            * @return: com.example.exam.common.ResultBean 
            * @Author: xhw
            * @Date: 2021/7/26 
            */ 
    public ResultBean updateRole(SysRoleBean srb,HttpServletRequest request){
        ResultBean result = new ResultBean();
        try {
            Integer i=srs.updateRole(srb);
            result.fillData(i);
            if(i == 1) {
                result.setCode(ResultBeanCodeEnum.OPERA_SUCCESS.getCode());
                result.setMsg(ResultBeanCodeEnum.OPERA_SUCCESS.getMsg());
            }else {
                result.setCode(ResultBeanCodeEnum.OPERA_FAIL.getCode());
                result.setMsg(ResultBeanCodeEnum.OPERA_FAIL.getMsg());
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(ResultBeanCodeEnum.OPERA_EXCEPTION.getCode());
            result.setMsg(ResultBeanCodeEnum.OPERA_EXCEPTION.getMsg());
        }
        return result;
    }
    @RequestMapping("/toDelete")
    @ResponseBody
        /** 
            * @Description: 批量删除角色信息
            * @Param: [request] 
            * @return: com.example.exam.common.ResultBean 
            * @Author: xhw
            * @Date: 2021/7/26 
            */ 
    public ResultBean toDelete(HttpServletRequest request) {
        ResultBean result = new ResultBean();
        String role_id  = request.getParameter("id");
        try {
            Integer deleState = null;
            if(StringUtils.isNotEmpty(role_id)) {
                int[] roleIds = StringToList.StrToArry(role_id);
                for(int i = 0; i < roleIds.length; i++) {
                    deleState = srs.deleteRole(roleIds[i]);
                }
                result.fillData(deleState);
                result.setCode(ResultBeanCodeEnum.OPERA_SUCCESS.getCode());
                result.setMsg(ResultBeanCodeEnum.OPERA_SUCCESS.getMsg());
            }else {
                result.setCode(ResultBeanCodeEnum.OPERA_FAIL.getCode());
                result.setMsg(ResultBeanCodeEnum.OPERA_FAIL.getMsg());
            }
            result.fillData(deleState);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(ResultBeanCodeEnum.OPERA_EXCEPTION.getCode());
            result.setMsg(ResultBeanCodeEnum.OPERA_EXCEPTION.getMsg());
        }
        return result;
    }
}
